package com.my.bld.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.File;

import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.jsoup.select.Evaluator.IsEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.my.bld.utils.Pager;
import com.my.bld.utils.UserUtils;
import com.opensymphony.xwork2.ModelDriven;
import java.util.*;

import com.my.bld.model.*;
import com.my.bld.dao.*;
import com.my.bld.service.*;

/**
 * @ClassName:  
 * @Description: 
 * @author administrator
 * @date 2015年12月24日 下午1:46:33 - 2017年02月28日 15时18分03秒
 */

@Controller("xsAction")
@Scope("prototype")
public class XsAction extends ActionSupport implements ModelDriven<Xs>{
	
	private static final long serialVersionUID = 1L;

	//==========model==============
	  private Xs xs;
		@Override
		public Xs getModel() {
			if(xs==null) xs = new Xs();
			return xs;	
		}
		//==========model==============
	/**
	 * 依赖注入 start dao/service/===
	 */
	@Autowired
	private XsService xsService;
	@Autowired
	private ShopService shopService;
	@Autowired
	private KcService kcService;
	@Autowired
	 private UserService userService;
	@Autowired
	private CgService cgService;
	
   private Integer old;
	
	//依赖注入 end  dao/service/===
	
	//-------------------------华丽分割线---------------------------------------------
	
	//============自定义参数start=============
	
	//============自定义参数end=============
	
	//-------------------------华丽分割线---------------------------------------------
	
	public Integer getOld() {
		return old;
	}
	public void setOld(Integer old) {
		this.old = old;
	}
	//依赖注入 end  dao/service/===
	
	//-------------------------华丽分割线---------------------------------------------
	
	//============自定义参数start=============
	
	//============自定义参数end=============
	
	//-------------------------华丽分割线---------------------------------------------
	
	//============文件上传start=======================================================
	private File file;
	//提交过来的file的名字
    private String fileFileName;
    //提交过来的file的MIME类型
    private String fileContentType;
   
    public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	 //============文件上传end=========================================================
			
	 //-------------------------华丽分割线---------------------------------------------//
	
	 //=============公=======共=======方=======法==========区=========start============//
	/**
	 * 列表分页查询
	 */
	public String xs(){
	    Map<String,Object> alias = new HashMap<String,Object>();
		StringBuffer sb = new StringBuffer();
		HttpSession session = ServletActionContext.getRequest()
				.getSession();
		Object attribute = session.getAttribute("role");
		
		int parseInt = Integer.parseInt(attribute.toString());
		if(parseInt == 1){//用户 只能看到自己的
			Integer loginId = UserUtils.getLoginId();
			sb = sb.append("from Xs where 1=1 and user.id = "+loginId);
		}else{
			sb = sb.append("from Xs where 1=1 ");
		}
		if(xs.getShop()!=null && xs.getShop().getType() !=null && !"".equals(xs.getShop().getType() )){
			sb.append("  and shop.type = :name ");
			alias.put("name",xs.getShop().getType());
		}
		sb = sb.append("order by id desc");
		Pager<Xs> pagers = xsService.findByAlias(sb.toString(),alias);
		ActionContext.getContext().put("pagers", pagers);
		ActionContext.getContext().put("Obj", xs);
		return SUCCESS;
    }
	
	/*
	 * 成本报表
	 */
	public String cb(){
		/*       System.out.println("年: " + );  
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");  
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));  
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));  
        System.out.println("分: " + now.get(Calendar.MINUTE));  
        System.out.println("秒: " + now.get(Calendar.SECOND));  */
	    Calendar now = Calendar.getInstance();  
        String year = now.get(Calendar.YEAR)+"";
        String yue =  (now.get(Calendar.MONTH) + 1) + "";
        String ri = now.get(Calendar.DAY_OF_MONTH)+""; 
        Map<String,Object> alias = new HashMap<String,Object>();
  		StringBuffer sb = new StringBuffer();
        if(xs.getYear() == null){
        	xs.setYear(year);
        }else{
        	
        }
        if(xs.getYue() == null){
        	xs.setYue(yue);
        }
      		sb = sb.append("from Cg where 1=1 and year ="+xs.getYear() +" and yue="+xs.getYue() );
      		sb = sb.append("  order by id desc");
      		Pager<Cg> pagers = cgService.findByAlias(sb.toString(),alias);
      		ActionContext.getContext().put("pagers", pagers);
      		ActionContext.getContext().put("Obj", xs);
      		//下面算总价格
      		List<Cg> listByAlias = cgService.listByAlias(sb.toString(), alias);
      		Double tot = 0.0;
      		if(listByAlias != null && listByAlias.size()>0){
      			for(Cg cg : listByAlias ){
      				tot += cg.getCbj()*cg.getNum();
      			}
      		}
      		ActionContext.getContext().put("tot", tot);
      		return SUCCESS;
	}
	
	
	/*
	 * 收入报表
	 */
	public String sr(){
		/*System.out.println("年: " + );  
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");  
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));  
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));  
        System.out.println("分: " + now.get(Calendar.MINUTE));  
        System.out.println("秒: " + now.get(Calendar.SECOND));  */
	    Calendar now = Calendar.getInstance();  
        String year = now.get(Calendar.YEAR)+"";
        String yue =  (now.get(Calendar.MONTH) + 1) + "";
        String ri = now.get(Calendar.DAY_OF_MONTH)+""; 
        Map<String,Object> alias = new HashMap<String,Object>();
  		StringBuffer sb = new StringBuffer();
        if(xs.getYear() == null){
        	xs.setYear(year);
        }else{
        	
        }
        if(xs.getYue() == null){
        	xs.setYue(yue);
        }
      		sb = sb.append("from Xs where 1=1 and year ="+xs.getYear() +" and yue="+xs.getYue() );
      		sb = sb.append("  order by id desc");
      		Pager<Xs> pagers = xsService.findByAlias(sb.toString(),alias);
      		ActionContext.getContext().put("pagers", pagers);
      		ActionContext.getContext().put("Obj", xs);
      		//下面算总价格
      		List<Xs> listByAlias = xsService.listByAlias(sb.toString(), alias);
      		Double tot = 0.0;
      		if(listByAlias != null && listByAlias.size()>0){
      			for(Xs xs : listByAlias ){
      				tot += xs.getXsj()*xs.getNum();
      			}
      		}
      		ActionContext.getContext().put("tot", tot);
      		return SUCCESS;
	}
	
	/**
	 * 利润
	 * @return
	 */
	public String lr(){
		/*       System.out.println("年: " + );  
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");  
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));  
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));  
        System.out.println("分: " + now.get(Calendar.MINUTE));  
        System.out.println("秒: " + now.get(Calendar.SECOND));  */
	    Calendar now = Calendar.getInstance();  
        String year = now.get(Calendar.YEAR)+"";
        String yue =  (now.get(Calendar.MONTH) + 1) + "";
        String ri = now.get(Calendar.DAY_OF_MONTH)+""; 
        Map<String,Object> alias = new HashMap<String,Object>();
  		StringBuffer sb = new StringBuffer();
        if(xs.getYear() == null){
        	xs.setYear(year);
        }else{
        	
        }
        if(xs.getYue() == null){
        	xs.setYue(yue);
        }
      		sb = sb.append("from Cg where 1=1 and isDelete = 0 and  year ="+xs.getYear() +" and yue="+xs.getYue() );
      		sb = sb.append("  order by id desc");
      		ActionContext.getContext().put("Obj", xs);
      		//下面算总价格
      		List<Cg> listByAlias = cgService.listByAlias(sb.toString(), alias);
      		Double tot = 0.0;
      		if(listByAlias != null && listByAlias.size()>0){
      			for(Cg cg : listByAlias ){
      				tot += cg.getCbj()*cg.getNum();
      			}
      		}
      		ActionContext.getContext().put("tot", tot);
      		
      		
      		 Map<String,Object> alias1 = new HashMap<String,Object>();
       		StringBuffer sb1 = new StringBuffer();
      		
      		sb1 = sb1.append("from Xs where 1=1 and year ="+xs.getYear() +" and yue="+xs.getYue() );
      		sb1 = sb1.append("  order by id desc");
      		ActionContext.getContext().put("Obj", xs);
      		//下面算总价格
      		List<Xs> listByAlias1 = xsService.listByAlias(sb1.toString(), alias1);
      		Double tot1 = 0.0;
      		if(listByAlias1 != null && listByAlias.size()>0){
      			for(Xs xs : listByAlias1 ){
      				tot1 += xs.getXsj()*xs.getNum();
      			}
      			ActionContext.getContext().put("tot1", tot1);
      		}
		return SUCCESS;
	}
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String add(){
		String hql = "from Shop where 1=1 and isDelete = 0";
		List<Shop> shops = shopService.listByAlias(hql, null);
		ActionContext.getContext().put("shops", shops);
		return SUCCESS;
	}
	
	/**
	 * 执行添加
	 * @return
	 */
	public String exAdd(){
		/*private Shop shop;
		
		private Double xsj;//销售价
		
		private Integer num;//销售数量
		
	    private Date createTime;//创建日期
	    
		private String year;
		
		private String yue;
		
		private String ri;*/
		  Calendar now = Calendar.getInstance();  
			/*       System.out.println("年: " + );  
			        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");  
			        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));  
			        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));  
			        System.out.println("分: " + now.get(Calendar.MINUTE));  
			        System.out.println("秒: " + now.get(Calendar.SECOND));  */
			        String year = now.get(Calendar.YEAR)+"";
			        String yue =  (now.get(Calendar.MONTH) + 1) + "";
			        String ri = now.get(Calendar.DAY_OF_MONTH)+""; 
			        //下面搞定库存
				    //首先判断这个shop在库存中有没有
				    String hql = "from Kc where shop.id = "+xs.getShop().getId();
				    Map<String ,Object> map = new HashMap<>();
				    map.put("id", xs.getShop().getId());
				    Kc byId = (Kc) kcService.queryObject(hql);
				    if(byId == null){
				    	/*Kc Kc = new Kc();
				    	Kc.setShop(shopService.getById(cg.getShop().getId()));
				    	Kc.setTotalNum(cg.getNum());
				    	kcService.save(Kc);*/
				    }else{
				    	byId.setTotalNum(byId.getTotalNum()- xs.getNum());
				    	kcService.update(byId);
				    	User byId2 = userService.getById(UserUtils.getLoginId());
				    	xs.setUser(byId2);
				    	   System.out.println(now.getTime()); 
				    	   xs.setYear(year);
				    	   xs.setYue(yue);
				    	   xs.setRi(ri);
				    	   xs.setCreateTime(new Date());
				    	   xsService.save(xs);
				    }
		ActionContext.getContext().put("url", "/xs_xs.do");
		return "redirect";
	}
	
	/**
	 * 查看详情页面
	 * @return
	 */
	public String view(){
		Xs n = xsService.getById(xs.getId());
		ActionContext.getContext().put("Obj", n);
		return SUCCESS;
	}
	
	/**
	 * 跳转修改页面
	 * @return
	 */
	public String update(){
		Xs n = xsService.getById(xs.getId());
		ActionContext.getContext().put("Obj", n);
		return SUCCESS;
	}
    
	/**
	 * 执行修改
	 * @return
	 */
	public String exUpdate(){
		//修改之前，先把仓库原来的删掉old个
		Xs n = xsService.getById(xs.getId());
				 String hql = "from Kc where shop.id = "+n.getShop().getId();
				    Map<String ,Object> map = new HashMap<>();
				    Kc byId = (Kc) kcService.queryObject(hql);
				    if(byId == null){
				    	
				    }else{
				    	byId.setTotalNum(byId.getTotalNum() + old);
				    	
				    }
				n.setNum(xs.getNum());
				n.setXsj(xs.getXsj());
				byId.setTotalNum(byId.getTotalNum()-xs.getNum());
				kcService.update(byId);
				xsService.update(n);
		ActionContext.getContext().put("url", "/xs_xs.do");
		return "redirect";
	}
	
	
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		Xs n = xsService.getById(xs.getId());
		 String hql = "from Kc where shop.id = "+n.getShop().getId();
		    Map<String ,Object> map = new HashMap<>();
		    Kc byId = (Kc) kcService.queryObject(hql);
		    if(byId == null){
		    }else{
		    	byId.setTotalNum(byId.getTotalNum() + n.getNum());
		    	kcService.update(byId);
		    }
		
		xsService.delete(xs.getId());
		ActionContext.getContext().put("url", "/xs_xs.do");
		return "redirect";
	}
	
	//=============公=======共=======方=======法==========区=========end============//
	
	 //-------------------------华丽分割线---------------------------------------------//
	
	 //=============自=======定=======义=========方=======法==========区=========start============//
	
	
	
	
	//=============自=======定=======义=========方=======法==========区=========end============//
		
	
	
}
